home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / mus / play / tracker_4_31.lzh / tracker / Arch / AF / audio.c next >
C/C++ Source or Header  |  1995-05-12  |  3KB  |  161 lines

  1. /* AF/audio.c
  2.     vi:ts=3 sw=3:
  3.  
  4. This code written by:
  5.  
  6. Andrew "Alf" Leahy                         email: alf@st.nepean.uws.edu.au
  7. University of Western Sydney - Nepean.
  8. Sydney, Australia.                         phone: (047) 360622 (work)
  9.  
  10.  
  11. Modified by Marc Espie to adjust to tracker 4.0 API.
  12.  
  13.  */
  14.  
  15. #include "defs.h"
  16. #include "extern.h"
  17. #include "/usr/local/include/AF/AFlib.h"
  18. ID("$Id: audio.c,v 4.7 1995/05/12 20:41:04 espie Exp espie $")
  19.  
  20. /* 0 external handset, 1 for internal speaker */
  21. #define SPEAKER 0
  22.      
  23. #define DEFAULT_SET_MIX
  24. #define SEPARATE_BUFFERS
  25.  
  26. #include "Arch/common.c"
  27.  
  28. LOCAL int ssize;
  29.  
  30. LOCAL ATime t, t_r, act, act_r;
  31. LOCAL AC ac, ac_r;
  32. LOCAL AFAudioConn *aud, *aud_r;
  33.  
  34. int sample_sizes[] = {
  35.     1,    /* MU255 */
  36.     1,    /* ALAW */
  37.     2,    /* Linear PCM, 16 bits, -1.0 <= x < 1.0 */
  38.     2,    /* Linear PCM, 32 bits, -1.0 <= x < 1.0 */
  39.     1,    /* G.721, 64Kbps to/from 32Kbps. */
  40.     1,    /* G.723, 64Kbps to/from 32Kbps. */
  41.     0
  42. };
  43.  
  44. int open_audio(int f, int s)
  45. {
  46.     AFSetACAttributes attributes;
  47.     int srate, device;
  48.     unsigned int channels;
  49.     AEncodeType type;
  50.     char *server;
  51.  
  52.     device = SPEAKER;
  53.     attributes.preempt = Mix;
  54.     attributes.start_timeout = 0;
  55.     attributes.end_silence = 0;
  56.     attributes.play_gain = 0;
  57.     attributes.rec_gain =  0;
  58.  
  59.     if ((server = (char *) getenv("AUDIOFILE")) == NULL)
  60.         end_all("Error: AUDIOFILE unset");
  61.     else
  62.     {
  63.         server = (char *) getenv("AUDIOFILE");
  64.         if ((aud = AFOpenAudioConn( server )) == NULL)
  65.             end_all("Error: can't open connection");
  66.         ac = AFCreateAC(aud, device, ACPlayGain, &attributes);
  67.         srate = ac->device->playSampleFreq;
  68.         type = ac->device->playBufType;
  69.         channels = ac->device->playNchannels;
  70.         ssize = sample_sizes[type] * channels;
  71.  
  72.         if ((buffer = (char *)malloc(ssize * srate)) == NULL)
  73.             end_all("Couldn't allocate play buffer");
  74.  
  75.         t = AFGetTime(ac);
  76.     }
  77.  
  78.     stereo=s;
  79.  
  80.     if (stereo)
  81.     {
  82.         server = (char *) getenv("AUDIORIGHT");
  83.         if ((aud = AFOpenAudioConn(server)) == NULL)
  84.             end_all("Error: can't open connection");
  85.         ac_r = AFCreateAC(aud, device, ACPlayGain, &attributes);
  86.         srate = ac->device->playSampleFreq;
  87.         type = ac->device->playBufType;
  88.         channels = ac->device->playNchannels;
  89.         ssize = sample_sizes[type] * channels;
  90.  
  91.         buffer_l = buffer;
  92.         if ((buffer_r = (char *)malloc(ssize * srate)) == NULL)
  93.             end_all("Couldn't allocate play buffer");
  94.         t_r = AFGetTime(ac_r);
  95.     }
  96.  
  97.     return srate;
  98. }
  99.  
  100. void set_synchro(int s)
  101. {
  102. }
  103.  
  104. int update_frequency()
  105. {
  106.     return 0;
  107. }
  108.  
  109. void output_samples(int left, int right)
  110.     {
  111.     if (stereo)
  112.         {
  113.         if (pms == 256)
  114.             {
  115.             buffer_l[idx] = linear2ulaw(left >>9);
  116.             buffer_r[idx++] = linear2ulaw(right >>9);
  117.             }
  118.         else
  119.             {
  120.             int s1, s2;
  121.  
  122.             s1 = (left+right)*pps;
  123.             s2 = (left-right)*pms;
  124.             buffer_l[idx] = linear2ulaw((s1 + s2) >>17);
  125.             buffer_r[idx++] = linear2ulaw((s1 - s2) >>17);
  126.             }
  127.         }
  128.     else /* mono */
  129.         buffer[idx++] = linear2ulaw((left + right) >>9);
  130.     }
  131.  
  132. void flush_buffer()
  133. {
  134.     act = AFPlaySamples(ac, t, idx, buffer);
  135.     t += idx/ssize;
  136.  
  137.     if (stereo) 
  138.     {
  139.         act_r = AFPlaySamples(ac_r, t_r, idx, buffer_r);
  140.         t_r += idx/ssize;
  141.     }
  142.     idx = 0;
  143. }
  144.  
  145. void discard_buffer()
  146. {
  147. }
  148.  
  149. void close_audio()
  150. {
  151.     free(buffer);
  152.  
  153. /* Alf: I'm not sure whether these functions are needed
  154.         I think these are Seg Faulting... */
  155.  
  156.      (void) AFCloseAudioConn(aud);
  157.  
  158.     if (stereo)
  159.         (void) AFCloseAudioConn(aud_r);
  160. }
  161.